package com.estasenposse.tdam.services;

import java.util.concurrent.ExecutionException;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RemoteViews;

import com.estasenposse.tdam.bd.DatabaseHandler;
import com.estasenposse.tdam.entities.Noticia;
import com.estasenposse.tdam.ui.Main;
import com.estasenposse.tdam.ui.NewActivity;
import com.estasenposse.tdam.ui.adapter.ImageDownloader;
import com.example.estasenposse.R;

public class MyWidgetProvider extends AppWidgetProvider {
	private static final String ACTION_CLICK = "ACTION_CLICK";
	private DatabaseHandler mHelper;

	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {
		Log.i("WatchWidget", "onUpdate");

		for (int wid : appWidgetIds) {
			if (wid == R.id.imageButton_widget_update) {
				Log.v(MyWidgetProvider.class.getName(), "R.id.update");
			} else if (wid == R.id.image_widget_ppal) {
				Log.v(MyWidgetProvider.class.getName(),
						"R.id.image_widget_ppal");
			}
		}
		// Get all ids
		ComponentName thisWidget = new ComponentName(context,
				MyWidgetProvider.class);

		int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);

		// int[] widgetId = allWidgetIds;
		mHelper = new DatabaseHandler(context);
		Noticia n = mHelper.getLastNote();

		RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
				R.layout.widget_layout);

		// Set the text and the image
		remoteViews.setTextViewText(R.id.update, n.getTitle());
		String url = n.getImage();

		ImageView iv = new ImageView(context);
		ProgressBar pb = new ProgressBar(context);

		pb.setId(R.id.pb_widget_image_ppal);
		iv.setId(R.id.image_widget_ppal);

		remoteViews.setProgressBar(pb.getId(), 0, 0, true);

		ImageDownloader downloader = new ImageDownloader(iv, pb);
		downloader.execute(url);

		try {
			remoteViews.setImageViewBitmap(R.id.image_widget_ppal,
					downloader.get());
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		pb.setVisibility(View.INVISIBLE);

		// Register an onClickListener

		if (n.getID() != null) {
			Intent intent = new Intent(context, NewActivity.class);
			intent.putExtra("ID", Integer.parseInt(n.getID()));
			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
					intent, PendingIntent.FLAG_UPDATE_CURRENT);
			remoteViews.setOnClickPendingIntent(R.id.layout_widget, pendingIntent);
		} else {
			Intent intent = new Intent(context, Main.class);
			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
					intent, PendingIntent.FLAG_UPDATE_CURRENT);
			remoteViews.setOnClickPendingIntent(R.id.layout_widget, pendingIntent);
		}

		Intent updateImage = new Intent(context, MyWidgetProvider.class);
		updateImage.setAction(ACTION_CLICK);

		PendingIntent update = PendingIntent.getBroadcast(context, 0,
				updateImage, PendingIntent.FLAG_UPDATE_CURRENT);

		remoteViews.setOnClickPendingIntent(R.id.imageButton_widget_update,
				update);

		appWidgetManager.updateAppWidget(thisWidget, remoteViews);
	}

	@Override
	public void onReceive(Context context, Intent intent) {
		Log.v(MyWidgetProvider.class.getName(), "on receive");
		if (intent.getAction().equals(ACTION_CLICK)) {
			AppWidgetManager manager = AppWidgetManager.getInstance(context);

			if (mHelper == null) {
				mHelper = new DatabaseHandler(context);
			}
			int[] asd = { 1 };
			onUpdate(context, manager, asd);
			Log.v(MyWidgetProvider.class.getName(), "on receive My Widget");
		} else {
			super.onReceive(context, intent);
		}

	}

	private String getLastNew(Context context) {
		Noticia n = mHelper.getLastNote();

		RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
				R.layout.widget_layout);

		// Set the text and the image
		remoteViews.setTextViewText(R.id.update, n.getTitle());
		String url = n.getImage();

		ImageView iv = new ImageView(context);
		ProgressBar pb = new ProgressBar(context);

		pb.setId(R.id.pb_widget_image_ppal);
		iv.setId(R.id.image_widget_ppal);

		remoteViews.setProgressBar(pb.getId(), 0, 0, true);

		ImageDownloader downloader = new ImageDownloader(iv, pb);
		downloader.execute(url);

		try {
			remoteViews.setImageViewBitmap(R.id.image_widget_ppal,
					downloader.get());
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return n.getID();
	}
}
